<?php

$userid         = get_loggedin_userid();
//$userid         = get_input('user_id');
$offset			= get_input('offset');
$limit			= get_input('limit');
$client_id		= 'opinions';

// get friends
/*
$query_friends = "SELECT guid_two FROM elgg_entity_relationships 
				WHERE guid_one = '$userid' 
				AND relationship =  '{\"app\":\"opinions\",\"relationship\":\"friend\"}' 
				LIMIT $offset, $limit";
*/

$query_friends = "SELECT friend_id FROM opinions_friends
					WHERE user_id = '$userid'";

$friend_ids = array();
$friends = get_data($query_friends);

foreach($friends as $friend) {
	$friend_ids[] = $friend->friend_id;
}

$friend_ids[] = $userid;

if(count($friend_ids) > 0) {

	$friends_id_string = '(';
	foreach($friend_ids as $friend_id) {
		$friends_id_string .= $friend_id . ',';
	}
	
	if(strlen($friends_id_string)>1) $friends_id_string = substr($friends_id_string, 0, -1);
	$friends_id_string .= ')';
}

$return_stream = array();

if(count($friend_ids) > 0) {
	$query_streams = "SELECT * FROM opinions_streams WHERE sender_id IN $friends_id_string ORDER BY id DESC";
	$streams = get_data($query_streams);
	
	$i = 0;
	foreach($streams as $stream) {
		
		$type_stream = $stream->type_id;
		
		if ($type_stream == '1') { // add_comment
		
			$userObject = get_user($stream->sender_id);
			$userObjectTwo = get_user($stream->receiver_id);
			$ratingObject = get_interest_rating_history($stream->ref_id);
			//$ratingObject = get_interest_rating($stream->ref_id);
			
			$return_stream[$i]['object_one'] = userToArray($userObject, $client_id, FALSE);
			$return_stream[$i]['object_two'] = userToArray($userObjectTwo, $client_id, FALSE);
			$return_stream[$i]['object_pref'] = to_rating_history_array($ratingObject);
			$return_stream[$i]['action'] = 'add_comment';
			$return_stream[$i]['count_comment'] = count_interest_rating_comment($stream->ref_id);
		
		} else if ($type_stream == '2') { // add_interest
		
			$userObject = get_user($stream->sender_id);
			$ratingObject = get_interest_rating_history($stream->ref_id);
			
			$return_stream[$i]['object_one'] = userToArray($userObject, $client_id, FALSE);
			$return_stream[$i]['object_two'] = to_rating_history_array($ratingObject);
			$return_stream[$i]['object_pref'] = array('');
			$return_stream[$i]['action'] = 'add_interest';
			$return_stream[$i]['count_comment'] = count_interest_rating_comment($stream->ref_id);
		
		} else if ($type_stream == '3') { // add_rating
			
			$userObject = get_user($stream->sender_id);
			$ratingObject = get_interest_rating_history($stream->ref_id);
			
			$return_stream[$i]['object_one'] = userToArray($userObject, $client_id, FALSE);
			$return_stream[$i]['object_two'] = to_rating_history_array($ratingObject);
			$return_stream[$i]['object_pref'] = array('');
			$return_stream[$i]['action'] = 'add_rating';
			$return_stream[$i]['count_comment'] = count_interest_rating_comment($stream->ref_id);
			
		}
		$return_stream[$i]['created_time'] = $stream->timestamp;
		$i++;
	}
}
/*
print '<pre>';
print_r($return_stream);
*/
echo json_encode(export_result(200, $return_stream));

?>